package bzqn.com.rongcloudsdk;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by a on 2017/5/9.
 */

public class DrawLineImageView extends android.support.v7.widget.AppCompatImageView {
    private int downX;
    private int downY;
    private int screenWidth;
    private int screenHeight;

    public void reset(){
        pointList.clear();
        lastClickCount = 0;
        clickCount = 0;
        invalidate();
    }

    public DrawLineImageView(Context context) {
        super(context);
        init();
    }

    public DrawLineImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawLineImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        screenWidth = MeasureSpec.getSize(widthMeasureSpec);
        screenHeight = MeasureSpec.getSize(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.WHITE);

    }

    public int getPixLengthWithP1(){
        if (pointList.size()>0){
            PointT t =  pointList.get(0);
            int lengthPx =  Math.abs(t.p2.y - t.p1.y);
            Log.i("PIX",lengthPx+":P1");
            return lengthPx;
        }
        return 1;
    }
    public int getPixLengthWithP2(){
        try {
            if (pointList.size()>=1){
                PointT t =  pointList.get(1);
                int lengthPx =  Math.abs(t.p2.y - t.p1.y);
                Log.i("PIX",lengthPx+":P2");
                return lengthPx;
            }
        }catch (Exception e){

        }

        return 1;
    }
    private Paint mPaint;
    static class PointT {
        public Point p1;
        public Point p2;
    }

    private List<PointT> pointList = new ArrayList<>();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < pointList.size(); i++) {
            PointT point = pointList.get(i);
            int pX = point.p1.x;
            int pY = point.p1.y;
            int left = (screenWidth-(screenWidth-pX));
            int top = screenHeight-(screenHeight-pY);
            int right = left+50;
            int bottom =  top+10;
            int finalLeft = left - (right-left)/2;
            RectF rect= new RectF(finalLeft,top,right,bottom);
            canvas.drawRoundRect(rect,3,3,mPaint);
            if (point.p2 != null) {
                int pX1 = point.p2.x;
                int pY1 = point.p2.y;
                int left1 = (screenWidth-(screenWidth-pX1));
                int top1 = screenHeight-(screenHeight-pY1);
                int right1 = left1+50;
                int bottom1 =  top1+10;
                int finalLeft1 = left1 - (right1-left1)/2;
                RectF rect1= new RectF(finalLeft1,top1,right1,bottom1);
                canvas.drawRoundRect(rect1,3,3,mPaint);
                //p1 line to p2

                canvas.drawLine(point.p1.x+10, point.p1.y+2, point.p2.x+10, point.p2.y, mPaint);
            }
        }

    }

    private int clickCount = 0;
    private int lastClickCount = 0;

    private boolean canTouch(){

        try {
            int x = pointList.get(1).p2.y;
            return false;
        }catch (Exception e){

        }
        return true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (canTouch()){
                    clickCount++;
                    downX = (int) event.getX();
                    downY = (int) event.getY();
                    PointT pointT = new PointT();
                    if (clickCount == 1) {
                        Point point = new Point();
                        point.x = downX;
                        point.y = downY;
                        pointT.p1 = point;
                        pointList.add(pointT);
                    }
                    if (clickCount == 2) {
                        PointT pt = pointList.get(lastClickCount);
                        Point p2 = new Point();
                        p2.x = downX;
                        p2.y = downY;
                        pt.p2 = p2;
                        clickCount = 0;
                        lastClickCount++;
                    }
                    invalidate();
                }

                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return super.onTouchEvent(event);
    }
}
